﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows;
#if WPF
using System.Windows.Markup;
#else
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml;
#endif

namespace Eternal.DataVisualization
{
#if WPF
    [ContentProperty("SeriesCollection")]
#else
    [ContentProperty(Name = "SeriesCollection")]
#endif
    public class AccumulativeChart : Chart
    {
        public NonAxisTypeCollection SeriesCollection
        {
            get { return (NonAxisTypeCollection)GetValue(SeriesCollectionProperty); }
            set { SetValue(SeriesCollectionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SeriesCollection.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SeriesCollectionProperty =
            DependencyProperty.Register("SeriesCollection", typeof(NonAxisTypeCollection), typeof(AccumulativeChart), new PropertyMetadata(new NonAxisTypeCollection(), OnSeriesCollectionChanged));

        
        /// Called when [Series collection changed].
        /// </summary>
        /// <param name="d">The d.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void OnSeriesCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as AccumulativeChart).OnSeriesCollectionChanged(e);
        }

        public AccumulativeChart()
        {
            UISeries = new SeriesCollection();
            SeriesCollection = new NonAxisTypeCollection();
        }

        /// <summary>
        /// Raises the <see>
        ///     <cref>E:SeriesCollectionChanged</cref>
        /// </see>
        ///     event.
        /// </summary>
        /// <param name="eventArgs">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private void OnSeriesCollectionChanged(DependencyPropertyChangedEventArgs eventArgs)
        {
            var newValue = eventArgs.NewValue as NonAxisTypeCollection;
            var oldValue = eventArgs.OldValue as NonAxisTypeCollection;
            if (newValue != null)
            {
                newValue.CollectionChanged += newValue_CollectionChanged;
            }
            if (oldValue != null)
            {
                oldValue.CollectionChanged -= newValue_CollectionChanged;
            }
        }

        void newValue_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    var axisSereis = e.NewItems[0] as NonAxisType;
                    UISeries.Add(axisSereis);
                }

                else if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                    var axisSereis = e.NewItems[0] as NonAxisType;
                    UISeries.Remove(axisSereis);
                }

                else if (e.Action == NotifyCollectionChangedAction.Reset)
                {
                    depthSeries.Clear();
                    UISeries.Clear();
                }
            }
            DynamicInvalidate();
        }
    }
}